import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * RabbitMQ 生产者
 * Created by liujiajia on 2017/6/30.
 */
public class Producer {
    public  static String QUEUE_NAME="rabbitMQ.test";
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置RabbitMQ相关信息
        factory.setHost("121.40.58.47");
        factory.setUsername("itadmin");
        factory.setPassword("123456");
        //factory.setPort(5672);
        //创建一个新的连接
        Connection connection = factory.newConnection();
        //创建一个通道
        Channel channel = connection.createChannel();
        //QUEUE_NAME=channel.queueDeclare().getQueue();
        //发送消息到队列中

        for (int i = 0; i < 500; i++) {
            //声明一个队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello RabbitMQ"+i;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
            System.out.println("Producer Send +'" + message + "'"+i);
        }

        //关闭通道和连接
        channel.close();
        connection.close();
    }
}
